#! /usr/bin/env python
# -*- coding: utf-8 -*-

from playwright.sync_api import Page
from ui_automation_test.common.base_page import BasePage
from locators.dualPrevention.riskGrading.riskClassification.loc_risk_classification_add import LocAddRiskClassification
from locators.dualPrevention.riskGrading.riskClassification.loc_risk_classification_edit import LocEditRiskClassification
from locators.dualPrevention.riskGrading.riskClassification.loc_risk_classification_search import LocSearchRiskClassification
from locators.dualPrevention.riskGrading.riskClassification.loc_risk_classification_view import LocViewRiskClassification
from locators.dualPrevention.riskGrading.riskClassification.loc_risk_classification_delete import LocDeleteRiskClassification    
from ui_automation_test.common.logger import logger


class AddRiskClassification(BasePage):   

    def __init__(self, page: Page):
        super().__init__(page)
        self.switch_menu('双重预防机制')
        self.switch_menu('风险分析清单管理')
        self.switch_menu('风险分级管控')       
    

    def add_risk_classification(self, object_name, unit_name, event_name, control_method, control_measure1, control_measure2, inspection_cycle, inspection_cycle_unit, control_measure_description, hazard_description):
        logger.info('点击新增按钮')
        # 只点击第一个"新增"按钮，避免匹配到表格内的"新增风险分级管控"按钮      
        self.page.get_by_role('button', name='新增').first.click()
        logger.info('选择所属风险分析对象')
        self.select(LocAddRiskClassification.loc_object_name, object_name)
        logger.info('选择所属风险分析单元')
        self.select(LocAddRiskClassification.loc_unit_name, unit_name)          
        logger.info('选择风险事件名称')
        self.select(LocAddRiskClassification.loc_event_name, event_name, exact=True)

        # 新增管控措施信息1
        logger.info('选择管控措施分类1')
        self.select(LocAddRiskClassification.loc_control_measure_category1, control_measure1)
        logger.info('选择管控措施分类2')
        self.select(LocAddRiskClassification.loc_control_measure_category2, control_measure2)
        logger.info('选择管控方式')
        self.select(LocAddRiskClassification.loc_control_method, control_method)
        logger.info('输入巡检周期')
        self.fill_number(LocAddRiskClassification.loc_inspection_cycle, inspection_cycle)
        logger.info('选择巡检周期单位')
        self.select(LocAddRiskClassification.loc_inspection_cycle_unit, inspection_cycle_unit, exact=True)
        logger.info('输入安全风险管控措施描述')
        self.fill(LocAddRiskClassification.loc_control_measure_description, control_measure_description)
        logger.info('输入隐患排查内容')
        self.fill(LocAddRiskClassification.loc_hazard_description, hazard_description)
        self.page.wait_for_timeout(1000)
        self.click(LocAddRiskClassification.loc_submit)
        self.page.wait_for_timeout(1000)

    def edit_risk_classification(self, control_measure1, control_measure2, control_method, inspection_cycle, inspection_cycle_unit, control_measure_description, hazard_description):
        logger.info('开始风险分级管控编辑测试')
        logger.info('点击编辑按钮')
        self.click(LocEditRiskClassification.loc_edit_button)
        logger.info('选择管控措施分类1')
        self.select(LocEditRiskClassification.loc_control_measure_category1, control_measure1)
        logger.info('选择管控措施分类2')
        self.select(LocEditRiskClassification.loc_control_measure_category2, control_measure2)
        logger.info('选择管控方式')
        self.select(LocEditRiskClassification.loc_control_method, control_method)
        logger.info('清除巡检周期')
        self.clear_by_backspace(LocEditRiskClassification.loc_inspection_cycle)
        logger.info('输入巡检周期')
        self.fill_number(LocEditRiskClassification.loc_inspection_cycle, inspection_cycle)
        logger.info('选择巡检周期单位')
        self.select(LocEditRiskClassification.loc_inspection_cycle_unit, inspection_cycle_unit, exact=True)
        logger.info('输入安全风险管控措施描述')
        self.fill(LocEditRiskClassification.loc_control_measure_description, control_measure_description)
        logger.info('输入隐患排查内容')
        self.fill(LocEditRiskClassification.loc_hazard_description, hazard_description)
        logger.info('提交')
        self.click(LocEditRiskClassification.loc_submit)
        

    def search_risk_classification(self, event_name, company, object_name, unit_name, risk_level):
        """
        查询风险事件
        :param event_name: 风险事件名称
        :param company: 所属企业
        :param object_name: 所属风险分析对象
        :param unit_name: 所属风险分析单元
        :param risk_level: 风险等级
        :param control_measure_category1: 管控措施分类1
        """
        logger.info('开始风险事件查询')
        if company:
            logger.info(f'选择所属企业: {company}')
            self.select(LocSearchRiskClassification.loc_company_select, company)
        if object_name:
            logger.info(f'选择所属风险分析对象: {object_name}')
            self.select(LocSearchRiskClassification.loc_object_select, object_name)
        if unit_name:
            logger.info(f'选择单元名称: {unit_name}')
            self.select(LocSearchRiskClassification.loc_unit_name_select, unit_name)
        if event_name:
            logger.info(f'选择风险事件名称: {event_name}')
            self.select(LocSearchRiskClassification.loc_event_name_select, event_name)
        if risk_level:
            logger.info(f'选择风险等级: {risk_level}')
            self.select(LocSearchRiskClassification.loc_risk_level_select, risk_level)
        logger.info('点击查询按钮')
        self.click(LocSearchRiskClassification.loc_search_button)

    def view_risk_classification(self, event_name, company, object_name, unit_name, risk_level):
        """
        查看风险事件详情
        :param event_name: 风险事件名称
        :param company: 所属企业
        :param object_name: 所属风险分析对象
        :param unit_name: 所属风险分析单元
        :param risk_level: 风险等级
        """
        logger.info('开始管控措施查看测试')
        # 查询出该风险事件下的所有管控措施
        self.search_risk_classification(event_name, company, object_name, unit_name, risk_level)
        self.page.wait_for_timeout(1000)
        logger.info('点击查看按钮')
        self.click(LocViewRiskClassification.loc_view_button)
        logger.info('查看管控措施详情')
        self.page.wait_for_timeout(6000)
        # 先定位到卡片区域
        card = self.page.locator('div:has-text("安全风险事件信息")')
        # 只选卡片内 class 为 el-button--primary 且文本为“返回”的按钮
        back_btn = card.locator('button.el-button--primary', has_text="返回")
        back_btn.scroll_into_view_if_needed()
        back_btn.click()

    def delete_risk_classification(self, event_name, unit_name):
        """
        根据风险事件名称和风险单元删除风险事件
        :param event_name: 风险事件名称
        :param unit_name: 所属风险分析单元
        :param control_measure_category1: 管控措施分类1
        """
        logger.info(f'准备删除风险事件：{event_name}下的管控措施')
        # 查询出该风险事件下的所有管控措施
        row = self.page.locator('.el-table__body tr').filter(has_text=event_name).filter(has_text=unit_name)
        # 点击第一行数据删除按钮
        row.first.get_by_text('删除').click()
        # 等待并点击确认弹窗的“确定”按钮
        self.page.get_by_role('button', name='确定').click()
        logger.info(f'删除 {event_name}下的管控措施完成')